/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "transferdescarcare.h"
#include <database.h>
#include "transferdescarcarepreturimodel.h"
#include "transferdescarcarespinboxdelegate.h"


TransferDescarcareObiecteInventar::TransferDescarcareObiecteInventar(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::transferdescarcareObiecteInventarForm()
{
	setupUi(this);

	Query * m_pozTransfer=defaultDatabase()->query("pozitiiTransferuriObiecteInventar");

	m_pozitiiDesQry=defaultDatabase()->query("pozitiiTransferDescarcareObiecteInventar");
	m_pozitiiDesQry->bindValue(":id_pozitie", m_pozTransfer->value( "id" ));
	m_pozitiiDesQry->bindValue(":id_reper", m_pozTransfer->value("id_reper"));
	m_pozitiiDesQry->bindValue(":id_act", m_pozTransfer->value( "id_act" ));
	m_pozitiiDesQry->bindValue(":data", m_pozTransfer->value( "data" ));
	m_pozitiiDesQry->select();
	m_pozitiiDesQry->remove();
	m_pozitiiDesQry->select();

	m_pozitiiIntQry=defaultDatabase()->query("pozitiiTransferIntrariObiecteInventar");
	m_pozitiiIntQry->bindValue(":id_pozitie", m_pozTransfer->value( "id" ));
	m_pozitiiIntQry->bindValue(":id_reper", defaultDatabase()->query("repereDesObiecteInventar")->value("id"));
	m_pozitiiIntQry->bindValue(":id_act", m_pozTransfer->value( "id_act" ));
	m_pozitiiIntQry->select();
	m_pozitiiIntQry->remove();
	m_pozitiiIntQry->select();

	m_pozitiiIntrariQry=defaultDatabase()->query("transferPreturiPozitiiIntrariObiecteInventar");
	m_pozitiiIntrariQry->bindValue("id_reper", m_pozTransfer->value("id_reper"));
	m_pozitiiIntrariQry->select();
	pozitii->setModel( new TransferDescarcarePreturiModel(this,m_pozitiiIntrariQry));
	pozitii->setItemDelegate( new TransferDescarcareSpinBoxDelegate(this) );
	connect(pozitii->model(), SIGNAL(modelReset()), pozitii, SLOT(setFocus()));
}

void TransferDescarcareObiecteInventar::closeEvent(QCloseEvent * event)
{
	for(int i=0;i<m_pozitiiIntrariQry->size();i++)
	{
		double cant=pozitii->model()->data(pozitii->model()->index(i,2)).toDouble();
		if (cant)
		{
			m_pozitiiDesQry->insert();
			m_pozitiiDesQry->setValue( "id_pozitie_intrari_obiecte_inventar",m_pozitiiIntrariQry->result()[i].value("id"));
			m_pozitiiDesQry->setValue( "pret",m_pozitiiIntrariQry->result()[i].value("pret"));
			m_pozitiiDesQry->setValue( "cantitate",cant);
			m_pozitiiDesQry->post();
			m_pozitiiIntQry->insert();
			m_pozitiiIntQry->setValue( "data",m_pozitiiIntrariQry->result()[i].value("data"));
			m_pozitiiIntQry->setValue( "pret",m_pozitiiIntrariQry->result()[i].value("pret"));
			m_pozitiiIntQry->setValue( "cantitate",cant);
			m_pozitiiIntQry->post();
		}
	}
	KontAMabilWindow::closeEvent(event);
}

TransferDescarcareObiecteInventar::~TransferDescarcareObiecteInventar()
{
}
